package com.tiankong.product.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.tiankong.product.dto.SpuInfoDto;
import com.tiankong.product.entity.*;
import com.tiankong.product.event.ProductCouponEvent;
import com.tiankong.product.mapper.SpuInfoMapper;
import com.tiankong.product.service.*;
import net.youdi.common.base.BaseServiceImpl;
import net.youdi.common.boot.ApplicationContexts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * spu信息 服务实现类
 * </p>
 *
 * @author scl
 * @since 2021-03-22
 */
@Service
public class SpuInfoServiceImpl extends BaseServiceImpl<SpuInfoMapper, SpuInfo> implements SpuInfoService {

    @Autowired
    private SpuInfoDescService spuInfoDescService;
    @Autowired
    private SpuAttrValueService spuAttrValueService;
    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private SkuImagesService skuImagesService;
    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;
    @Override
    public void saveSpuInfo(SpuInfoDto spuInfoDto) {
        //1、保存spu基本信息 t_spu_info
        save(spuInfoDto);
        //2、保存Spu的描述图片 t_spu_info_desc
        SpuInfoDesc spuInfoDesc = new SpuInfoDesc().setSpuId(spuInfoDto.getId()).setDecript(spuInfoDto.getDecript()).setImages(spuInfoDto.getImages());
        spuInfoDescService.save(spuInfoDesc);
        //3、保存spu的规格参数;t_spu_attr_value
        List<Attr> attrs = spuInfoDto.getBaseAttrs();
        List<SpuAttrValue> collect = attrs.stream().map(attr -> {
            SpuAttrValue spuAttrValue = new SpuAttrValue();
            return spuAttrValue.setAttrId(attr.getAttrId())
                        .setAttrName(attr.getAttrName())
                        .setAttrValue(attr.getValueSelect())
                        .setQuickShow(attr.getShowDesc())
                        .setSpuId(spuInfoDto.getId());
        }).collect(Collectors.toList());
        spuAttrValueService.saveBatch(collect);

        //4.保存当前spu对应的所有sku信息；
        List<SkuInfo> skus = spuInfoDto.getSkus();
        if (CollectionUtil.isNotEmpty(skus)){
            skus.forEach(s->s.setSpuId(spuInfoDto.getId()).setCatalogId(spuInfoDto.getCatalogId())
                    .setBrandId(spuInfoDto.getBrandId()).setSaleCount(0L));
            //4.1 保存sku基本信息
            skuInfoService.saveBatch(skus);
            //4.2 保存sku图片信息
            List<SkuImages> skuImages = new ArrayList<>();
            //4.3 sku的销售属性信息：t_sku_sale_attr_value
            List<SkuSaleAttrValue> saleAttrValues = new ArrayList<>();

            skus.forEach(s->{
                List images = s.getImages();
                if (CollectionUtil.isNotEmpty(images)){
                    skuImages.add(new SkuImages().setSkuId(s.getSkuId()).setImages(images));
                }
                List<SkuSaleAttrValue> skuSaleAttrValues = s.getAttrs();
                if (CollectionUtil.isNotEmpty(skuSaleAttrValues)){
                    skuSaleAttrValues.forEach(a->saleAttrValues.add(a.setSkuId(s.getSkuId())));
                }
            });
            if (CollectionUtil.isNotEmpty(skuImages)){
                skuImagesService.saveBatch(skuImages);
            }
            if (CollectionUtil.isNotEmpty(saleAttrValues)){
                skuSaleAttrValueService.saveBatch(saleAttrValues);
            }
        }

        ApplicationContexts.getApplicationContext().publishEvent(new ProductCouponEvent(spuInfoDto));
    }
}
